package com.iraeta.controller.kcpd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.Config;
import com.iraeta.common.ConfigCache;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.ErpCurrentStore;
import com.iraeta.entity.main.Etllog;
import com.iraeta.entity.main.vo.ErpCurrentStoreVo;
import com.iraeta.service.basic.CurrentStoreService;
import com.iraeta.service.etl.ErpEtlService;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@RestController(value = "CurrentStoreController")
@RequestMapping("currentstore")
public class CurrentStoreController extends BaseController {
    @Autowired
    CurrentStoreService currentStoreService;
    @Autowired
    ErpEtlService erpEtlService;
    @Autowired
    ConfigCache configCache;
    @Autowired
    Config config;

    @RequestMapping("pagecurrentstore")
    public String pageCurrentStore(@RequestBody JSONObject jo){
        String cwhname = jo.getString("cwhname");
        String cinvcode = jo.getString("cinvcode");
        String cinvname = jo.getString("cinvname");
        String cinvcname = jo.getString("cinvcname");
        String cinvstd = jo.getString("cinvstd");
        Integer iquantityflag = (jo.getBoolean("iquantityflag") == null || !jo.getBoolean("iquantityflag"))  ? null : 1;
        Integer czquantityflag = (jo.getBoolean("czquantityflag") == null || !jo.getBoolean("czquantityflag"))  ? null : 1;
        Integer barcodeflag = (jo.getBoolean("barcodeflag") == null || !jo.getBoolean("barcodeflag"))  ? null : 1;
        ErpCurrentStoreVo erpCurrentStoreVo = new ErpCurrentStoreVo(cwhname, cinvcode, cinvname, cinvcname, cinvstd);
        erpCurrentStoreVo.setUserid(this.getUserId());
        erpCurrentStoreVo.setIquantityflag(iquantityflag);
        erpCurrentStoreVo.setCzquantityflag(czquantityflag);
        erpCurrentStoreVo.setBarcodeflag(barcodeflag);
        this.pageSet(jo);
        List<ErpCurrentStore> ErpCurrentStoreList = currentStoreService.listErpCurrentStoreForPrint(erpCurrentStoreVo);
        PageInfo<ErpCurrentStore> erpCurrentStorePageInfo = this.getPageInfo(ErpCurrentStoreList);
        return JSON.toJSONString(Result.success(erpCurrentStorePageInfo));
    }

    /*查看现存量的盘点数据*/
    @RequestMapping("pagepdnow")
    public String pagePdNow(@RequestBody JSONObject jo){
        String cwhname = jo.getString("cwhname");
        String cinvcode = jo.getString("cinvcode");
        String cinvname = jo.getString("cinvname");
        String cinvstd = jo.getString("cinvstd");
        Integer iquantityflag = (jo.getBoolean("iquantityflag") == null || !jo.getBoolean("iquantityflag"))  ? null : 1;
        Integer pdflag = (jo.getBoolean("pdflag") == null || !jo.getBoolean("pdflag"))  ? null : 1;
        Integer czquantityflag = (jo.getBoolean("czquantityflag") == null || !jo.getBoolean("czquantityflag"))  ? null : 1;
        Integer npdflag = (jo.getBoolean("npdflag") == null || !jo.getBoolean("npdflag"))  ? null : 1;
        ErpCurrentStoreVo erpCurrentStoreVo = new ErpCurrentStoreVo(cwhname, cinvcode, cinvname, "", cinvstd);
        erpCurrentStoreVo.setUserid(this.getUserId());
        erpCurrentStoreVo.setIquantityflag(iquantityflag);
        erpCurrentStoreVo.setPdflag(pdflag);
        erpCurrentStoreVo.setNpdflag(npdflag);
        erpCurrentStoreVo.setCzquantityflag(czquantityflag);
        this.pageSet(jo);
        List<ErpCurrentStore> ErpCurrentStoreList = currentStoreService.listErpCurrentStore(erpCurrentStoreVo);
        PageInfo<ErpCurrentStore> erpCurrentStorePageInfo = this.getPageInfo(ErpCurrentStoreList);
        return JSON.toJSONString(Result.success(erpCurrentStorePageInfo));
    }

    /*导出现存量的盘点数据*/
    @RequestMapping("exportpdnow")
    public String exportPdNow(@RequestBody JSONObject jo){
        String cwhname = jo.getString("cwhname");
        String cinvcode = jo.getString("cinvcode");
        String cinvname = jo.getString("cinvname");
        Integer iquantityflag = (jo.getBoolean("iquantityflag") == null || !jo.getBoolean("iquantityflag"))  ? null : 1;
        Integer pdflag = (jo.getBoolean("pdflag") == null || !jo.getBoolean("pdflag"))  ? null : 1;
        Integer czquantityflag = (jo.getBoolean("czquantityflag") == null || !jo.getBoolean("czquantityflag"))  ? null : 1;
        Integer npdflag = (jo.getBoolean("npdflag") == null || !jo.getBoolean("npdflag"))  ? null : 1;
        ErpCurrentStoreVo erpCurrentStoreVo = new ErpCurrentStoreVo(cwhname, cinvcode, cinvname);
        erpCurrentStoreVo.setUserid(this.getUserId());
        erpCurrentStoreVo.setIquantityflag(iquantityflag);
        erpCurrentStoreVo.setPdflag(pdflag);
        erpCurrentStoreVo.setNpdflag(npdflag);
        erpCurrentStoreVo.setCzquantityflag(czquantityflag);
        List<ErpCurrentStore> ErpCurrentStoreList = currentStoreService.listErpCurrentStore(erpCurrentStoreVo);
        String fileName = "库存盘点-" + Util.getBusinessNo() + ".xlsx";
        String filePath = config.profile + "/temp/" + fileName;
        currentStoreService.exportErpCurrentStore(filePath, ErpCurrentStoreList);
        return this.success(fileName);
    }

    @RequestMapping("clearkc")
    public String clearKc(@RequestBody JSONObject jo){
        String cwhname = jo.getString("cwhname");
        currentStoreService.clearKc(cwhname);
        return this.success();
    }

    @RequestMapping("clearselectkc")
    public String clearSelectKc(@RequestBody JSONObject jo){
        JSONArray selectedAutoIdArray = jo.getJSONArray("selectedAutoIdArray");
        currentStoreService.clearSelectKc(selectedAutoIdArray);
        return this.success();
    }

    @RequestMapping("getcurrentstore")
    public String getCurrentStore(@RequestBody JSONObject jo){
        Long id = jo.getLongValue("id");
        ErpCurrentStore erpCurrentStore = currentStoreService.getErpCurrentStore(id);
        return this.success(erpCurrentStore);
    }

    @RequestMapping("savecurrentstore")
    public String saveCurrentStore(@RequestBody JSONObject jo){
        Long id = jo.getLongValue("id");
        ErpCurrentStore erpCurrentStore = currentStoreService.getErpCurrentStore(id);
        BigDecimal pdquantity = new BigDecimal(jo.getString("pdquantity")) ;
        erpCurrentStore.setPdquantity(pdquantity);
        erpCurrentStore.setLasttbdate(new Date());
        erpCurrentStore.setPduser(this.getUserName());
        erpCurrentStore.setCzquantity(erpCurrentStore.getPdquantity().subtract(erpCurrentStore.getIquantity()));
        currentStoreService.updatePdByPrimaryKey(erpCurrentStore);
        return this.success();
    }

    @RequestMapping("addprintcount")
    public String addPrintCount(@RequestBody JSONObject jo){
        JSONArray idsJa = jo.getJSONArray("ids");
        currentStoreService.addPrintCount(idsJa);
        return this.success();
    }

    @RequestMapping("shujutongbu")
    public String shuJuTongBu(){
        Etllog etllog = erpEtlService.processErpCurrentStore();
        return this.success(etllog);
    }
}
